package br.pesquisa.lsystem.gui;

import processing.core.PApplet;
import br.pesquisa.lsystem.Celula;
import br.pesquisa.lsystem.Interpretador;

public class ProcessingCellPanel extends CellPanel {
	private static final long serialVersionUID = 5544806466116273018L;

	private ProcessingPanel panel;

	public ProcessingCellPanel(MainFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	public void doStart() {
		panel.start();
	}

	@Override
	public void doStop(Interpretador<Celula> interpretador) {
		//panel.stop();
	}

	@Override
	public void doUpdate(Interpretador<Celula> interpretador) {
	}

	@Override
	public void init() {
		panel = new ProcessingPanel();
		add(panel);
		panel.init();
	}

	@SuppressWarnings("serial")
	private class ProcessingPanel extends PApplet {
		private static final long serialVersionUID = -7346742896591780596L;

		int xgridcount = 4; 
		int ygridcount = 4; 
		int zgridcount = 4; 
		int halfx = xgridcount / 2; 
		int halfy = ygridcount / 2; 
		int halfz = zgridcount / 2;   
		float a = 0.0f;   
		float boxsize = 0; 
		int gDistance;

		@Override
		public void setup() {
			size(200, 200, P3D);
			noStroke();
			colorMode(HSB, 100);
			gDistance = width - width / 4;
		}

		@Override
		public void draw() {
			background(51);

			translate(halfx * gDistance - gDistance, halfy * gDistance
					- gDistance, -gDistance * 10);

			// First number is pauses between timechange,
			// lower 2nd number = faster speed
			a += abs(sin(millis() / 1500) / 500);

			for (int i = halfx * -1; i <= halfx; i += 1) {
				translate(gDistance, 0, 0);
				rotateX(a * .9f);

				pushMatrix();
				for (int j = halfy * -1; j <= halfy; j += 1) {
					translate(-width / 2, gDistance, 0);
					rotateZ(a * 1.1f);
					pushMatrix();
					for (int k = halfz * -1; k <= halfz; k += 1) {
						translate(0, 0, gDistance);
						rotateY(a);

						boxsize = 30 + (sin(i * j * k * a) * (gDistance) * 1.66f);

						fill(0, 0, 100 - boxsize / 5.1f);
						box(boxsize, boxsize, boxsize);
					}
					popMatrix();
				}
				popMatrix();
			}
		}
	}
}
